﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.Linq;
using System.Diagnostics;
using System.Data;
namespace MixScriptWeb.Share
{
    public partial class MainMenu : System.Web.UI.MasterPage
    {
      
       private static List<TreeNode> lt ;//= new List<TreeNode>();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //    Session["userid"] = null;
                //   Session["userid"] = "";

                if (Session["userid"].ToString() != string.Empty)
                {
                    if (!Page.IsPostBack)
                    {
                        hlUSername.Text = Session["userid"].ToString();
                        lt = new List<TreeNode>();
                        initMenu();
                        myTvMenu.CollapseAll();
                    }
                }
                else
                {
                    Response.Redirect("~/Default.html");
                }
            }
            catch (Exception ex)
            {
                Response.Redirect("~/Default.html");
                lblError.Text = ex.Message;
                statusbar.Visible = true;
            }
        }


        
        #region InitMenu
        private bool cekParent(string nodeid, string surl, DataTable dt)
        {
            if (dt.Rows.Count == 0)
                return true;

            foreach (DataRow dr in dt.Rows)
            {
                string s = nodeid;
                if (s == "2215")
                    s += s;


                if (dr["nodeid"].ToString() == nodeid)
                {
                    if (dr["parentid"].ToString() == "1")
                    {
                        return true;

                    }

                }
            }

            return false;
        }
        private bool cekmenu(string nodeid, string surl, DataTable dt)
        {

            if (dt.Rows.Count == 0)
                return true;

            foreach (DataRow dr in dt.Rows)
            {
                string s = nodeid;
                if (s == "112")
                    s += s;


                if (dr["nodeid"].ToString() == nodeid)
                {

                    if (dr["tick"].ToString() == "1")
                    {
                        if (surl.Trim() != string.Empty)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else if (dr["parentid"].ToString() == "1")
                    {
                        return true;                     

                    }
                    else
                    {
                        return false;
                    }
                }
            }


            return false;
        }
        private void PrintRecursive(TreeNode treeNode,DataTable dt)
        {        
            foreach (TreeNode tn in treeNode.ChildNodes)
            {
                string s = tn.Value;
                if(!cekmenu(tn.Value,tn.NavigateUrl, dt))
                {
                    lt.Add(tn);

                    
                }

                PrintRecursive(tn,dt);
            }
        }
        private void PrintRecursiveP(TreeNode treeNode, DataTable dt)
        {
            foreach (TreeNode tn in treeNode.ChildNodes)
            {
                string s = tn.Value;
                if (cekParent(tn.Value,tn.NavigateUrl, dt))
                {
                    if(tn.ChildNodes.Count==0)
                    lt.Add(tn);

                }
                PrintRecursiveP(tn, dt);
            }
        }
        private void filtermenuParent(TreeView tv, DataTable dt)
        {
            try
            {
                PrintRecursiveP(tv.Nodes[0], dt);
                PrintRecursiveP(tv.Nodes[1], dt);
                PrintRecursiveP(tv.Nodes[2], dt);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        private void filtermenu(TreeView tv,DataTable dt)
        {
            try
            {
                PrintRecursive(tv.Nodes[0],dt);
                PrintRecursive(tv.Nodes[1],dt);
                PrintRecursive(tv.Nodes[2],dt);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        void initMenu()
        {
            try
            {
                ExtractRightMenu xrm = new ExtractRightMenu(Session["userid"].ToString());
                TreeNode[] tns = xrm.AksesMenu("MixScript/MAINMENU", Page);
                TreeView tv = new TreeView();
                if ((tns != null) && (tns.Length > 0))
                {
                    int x = 0;
                    foreach (TreeNode tn in tns)
                    {
                        myTvMenu.Nodes.Add(tn);

                    }
                    if (Session["userid"].ToString().ToUpper() == "YANUSAPU")
                        return;

                    filtermenu(myTvMenu, xrm.DtPriv);
                    if (lt.Count > 0) 
                    foreach (TreeNode tn in lt)
                    {
                        TreeNode tn2 = tn.Parent;
                        tn2.ChildNodes.Remove(tn);
                    }
                    //returnf

                    for (int i = 0; i < 4; i++)
                    {
                        lt.Clear();
                        filtermenuParent(myTvMenu,xrm.DtPriv);

                        if (lt.Count > 0)
                            foreach (TreeNode tn in lt)
                            {
                                TreeNode tn2 = tn.Parent;
                                tn2.ChildNodes.Remove(tn);
                            }
                    }
                    
                  
                    
                    tns = null;
                }
                else
                {
                    Response.Write(xrm.ErrorReturn);
                }



            }
            catch (Exception ex)
            {

                lblError.Text = ex.Message;
            }
        }
        #endregion





    }
}